//
//  MapViewController.m
//  OrderSeat
//
//  Created by 王威 on 14-1-4.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "MapViewController.h"
#import "MapView.h"
#import "CalloutMapAnnotation.h"
#import "JingDianMapCell.h"
#import "BasicMapAnnotation.h"
#define span 40000

@interface MapViewController ()
{
    NSMutableArray *_annotationList;
    
    CalloutMapAnnotation *_calloutAnnotation;
	CalloutMapAnnotation *_previousdAnnotation;
}

@property (strong, nonatomic) MapView *mapView;
@property (strong, nonatomic) Place *from;
@property (strong, nonatomic) Place *to;

@end

@implementation MapViewController

- (id)initWithShopInfo:(NSDictionary *)shopEntity
{
    self = [super initWithNibClass:[MapViewController class]];
    if (self) {
        self.shopEntity = shopEntity;
        Place *place = [[Place alloc] init];
        place.logo = [_shopEntity objectForKey:@"logo"];
        place.longitude = [[_shopEntity objectForKey:@"jingdu"] floatValue];
        place.latitude = [[_shopEntity objectForKey:@"weidu"] floatValue];
        place.name = [_shopEntity objectForKey:@"name"];
        place.description = [_shopEntity objectForKey:@"xianqu"];
        self.to = place;
        
        [AppDelegate sharedInstance].locactionDelegate = self;
        [[AppDelegate sharedInstance] startLocation];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self setNavigationTitle:@"地图"];
    
    _mapView = [[MapView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _mapView.parentVC = self;
    [self.view addSubview:_mapView];

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAct:)];
    longPress.minimumPressDuration = 1;
    [_mapView addGestureRecognizer:longPress];
}

- (void)leftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)longPressAct:(UILongPressGestureRecognizer*)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        CGPoint touchPoint = [recognizer locationInView:_mapView.mapView];
        CLLocationCoordinate2D coordinate = [_mapView.mapView convertPoint:touchPoint toCoordinateFromView:_mapView.mapView];
        _to.longitude = coordinate.longitude;
        _to.latitude = coordinate.latitude;
        [_mapView showRouteFrom:_from to:_to];
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
{
    _from = [[Place alloc] init] ;
	_from.name = @"当前位置";
	_from.latitude = [[AppSetting latitude] floatValue]; //newLocation.coordinate.latitude;
	_from.longitude = [[AppSetting longitude] floatValue];// newLocation.coordinate.longitude;
	
    NSLog(@"%@", _to);
	[_mapView showRouteFrom:_from to:_to];
}

@end
